<template>
  <div class="bankCardInfo">
    <van-nav-bar
        :title="$t('add_bank_card')"
        left-arrow
        @click-left="$router.go(-1)"
    />
    <div style="padding: 10px">
      <div class="content_list">
        <!--        收款银行-->
        <div class="content_item" @click="selectBankClick">
          <div>
            <img class="image" src="../../assets/icon_receiving_bank.png">
            <span class="text">{{ $t('receiving_bank') }}</span>
          </div>
          <div class="select_right">
            <span v-if="cardIssuingBank" class="select_right_text">{{ cardIssuingBank }}</span>
            <span v-else class="select_right_text">{{ $t('hint_select_receiving_bank') }}</span>
            <van-icon name="arrow" color="#9D9D9D"/>
          </div>
        </div>
        <!--        持卡人-->
        <div class="content_item">
          <div>
            <img class="image" src="../../assets/icon_provider_one.png">
            <span class="text">{{ $t('card_holder') }}</span>
          </div>
          <div class="select_right">
            <input class="input" readonly="readonly" v-model="userName"/>
          </div>
        </div>
        <!--        银行账户-->
        <div class="content_item">
          <div>
            <img class="image" src="../../assets/icon_bank_account.png">
            <span class="text">{{ $t('bank_account') }}</span>
          </div>
          <div class="select_right">
            <input class="input" v-model="bankcard" :placeholder="$t('hint_input_bank_account')"/>
          </div>
        </div>
        <!--        银行所在省份-->
        <div class="content_item" @click="bankProvinceClick">
          <div>
            <img class="image" src="../../assets/icon_bank_account.png">
            <span class="text">{{ $t('bank_province') }}</span>
          </div>
          <div class="select_right">
            <span v-if="bankProvinceText" class="select_right_text">{{ bankProvinceText }}</span>
            <span v-else class="select_right_text">{{ $t('hint_select_bank_province') }}</span>
            <van-icon name="arrow" color="#9D9D9D"/>
          </div>
        </div>
        <!--        银行支行名称-->
        <div class="content_item">
          <div>
            <img class="image" src="../../assets/icon_bank_branch.png">
            <span class="text">{{ $t('bank_branch_name') }}</span>
          </div>
          <div class="select_right">
            <input class="input" v-model="branchName" :placeholder="$t('hint_input_bank_branch_name')"/>
          </div>
        </div>
        <!--        设置为默认收款银行卡-->
        <div class="content_item">
          <div>
            <span class="text">{{ $t('bank_set_default_receiving_bank') }}</span>
          </div>
          <div class="select_right">
            <van-switch active-color="#0F9A0F" v-model="isSelected"/>
          </div>
        </div>
      </div>
      <div class="dialog" v-show="selectBankShow" @click.self="selectBankShow = false">
        <div class="dialog_body">
          <div class="list_item" v-for="item in selectBankList" :key="item.id" @click="selectBankChange(item)">
            <img class="image" :src="item.logoUrl"/>
            <div class="text">{{ item.bankName }}</div>
            <div class="active">
              <img v-show="cardIssuingBank === item.bankName" src="../../assets/icon_blue_hook.png">
            </div>
          </div>
        </div>
      </div>
      <!--      选择省份-->
      <div class="dialog" v-show="bankProvinceShow" @click.self="bankProvinceShow = false">
        <div class="dialog_body">
          <div class="list_item" v-for="item in bankProvinceList" :key="item.id" @click="bankProvinceChange(item)">
            <div class="text" style="padding: 10px 0">{{ item.name }}</div>
            <div class="active">
              <img v-show="provinces === item.id" src="../../assets/icon_blue_hook.png">
            </div>
          </div>
        </div>
      </div>
      <div class="confirm">
        <button @click="confirm" >{{ $t('finish') }}</button>
      </div>
    </div>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from 'vue'
import {fetchBankCard, selectCitys, addBankCard} from '@/api/home'
import {useI18n} from 'vue-i18n'
import {useRoute} from 'vue-router'
import {useStore} from 'vuex'
import {Toast} from "vant";
import router from "@/router";
import {debounce, processNext} from '@/utils/tool'

export default {
  setup() {
    const {tm} = useI18n()
    const store = useStore()
    let cardInfo = reactive({
      isSelected: false,
      cardIssuingBank: null,
      provinces: null,
      userName: '',
      branchName: '',
      bankcard: ''
    })

    const routes = useRoute()
    // 选择收款银行
    const selectBankSheet = reactive({
      selectBankShow: false,
      selectBankText: '',
      selectBankList: [],
      selectBankChange: ({bankName}) => {
        cardInfo.cardIssuingBank = bankName
        selectBankSheet.selectBankShow = false
      },
      selectBankClick: async () => {
        selectBankSheet.selectBankShow = true
      }
    })
    // 选择省
    const bankProvinceSheet = reactive({
      bankProvinceShow: false,
      bankProvinceText: '',
      bankProvinceList: [],
      bankProvinceChange: ({name, id}) => {
        cardInfo.provinces = id
        bankProvinceSheet.bankProvinceText = name
        bankProvinceSheet.bankProvinceShow = false
      },
      bankProvinceClick: async () => {
        bankProvinceSheet.bankProvinceShow = true
      }
    })
    const verificationInfo = () => {
      let message = ''
      const {cardIssuingBank, provinces, userName, branchName, bankcard} = cardInfo
      if (!cardIssuingBank) return message = tm('hint_select_receiving_bank')
      if (!userName) return message = tm('hint_input_name')
      if (!bankcard) return message = tm('hint_input_bank_account')
      if (!provinces) return message = tm('hint_select_bank_province')
      if (!branchName) return message = tm('hint_input_bank_branch_name')
      return message
    }
    const confirm = debounce(
        async () => {
          if (!verificationInfo()) {
            /** 没有添加过银行卡 设置默认收款银行
             * bankCardInfo true 已填写过 false未填写
             * @type {any}
             */
            const userInfoVerify = JSON.parse(localStorage.getItem('userInfoVerify'))
            const {bankCardInfo} = userInfoVerify
            let isSelected = cardInfo.isSelected ? 1 : 0
            if (!bankCardInfo) {
              isSelected = 1
            }
            const res = await addBankCard({
              ...cardInfo,
              isSelected:isSelected,
            })
            if (res.code === 1) {
              Toast({
                message: tm('submit_success')
              })
              // 新增收款银行进来 跳转我的收款银行
              if (router.currentRoute.value.query.isAdd === '1') {
                router.push({
                  path: '/myCard',
                  query: {
                    backName: routes.query.backName
                  }
                })
              } else {
                processNext('bankCardInfo', routes.query.backName)
              }
            }
          } else {
            Toast({
              message: verificationInfo()
            })
          }
        }
    )
    onMounted(() => {
      const name = JSON.parse(localStorage.getItem('userWriteInfo')).name
      cardInfo.userName = name
    })
    const getSelect = async () => {
      // 获取银行卡列表
      const bankCardRes = await fetchBankCard()
      if (bankCardRes.code === 1) {
        selectBankSheet.selectBankList = bankCardRes.data
      }
      // 获取省
      const provincesRes = await selectCitys({pid: 0})
      if (provincesRes.code === 1) {
        bankProvinceSheet.bankProvinceList = provincesRes.data
      }
      // 清除loading
      store.dispatch('changeLoading', false)
    }
    getSelect()
    return {
      ...toRefs(cardInfo),
      ...toRefs(selectBankSheet),
      ...toRefs(bankProvinceSheet),
      confirm
    }
  }
}
</script>

<style lang="less" scoped>
.bankCardInfo {
  background-color: #f2f3f5;
  overflow: auto;

  ::v-deep .van-nav-bar {
    background: linear-gradient(90deg, #FF5252, #FF8244) !important;
    z-index: 1999;
  }

  ::v-deep .van-nav-bar .van-icon {
    color: #333333 !important;
  }

  ::v-deep .van-switch {
    height: 26px !important;

    .van-switch__node {
      height: 26px !important;
    }
  }

  .content_list {
    .content_item {
      height: 40px;
      border-bottom: solid 1px #f2f3f5;
      background-color: #ffffff;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 10px;

      div {
        height: 100%;
        display: flex;
        align-items: center;

        .image {
          height: 25px;
          width: 25px;
          margin-right: 10px;
        }

        .text {
          font-size: 12px;
          font-weight: 600;
        }
      }

      .select_right {
        color: #9D9D9D;
        font-size: 12px;
        margin-left: 5px;
        flex: 1;
        justify-content: flex-end;
      }

      .select_right_text {
        color: #9D9D9D;
        font-size: 12px;
        margin-right: 5px;
      }

      .input {
        color: #6f6c6c;
        border: none;
        height: 25px;
        font-size: 12px;
        text-align: right;
      }
    }
  }

  .confirm {
    margin-top: 20px;
    text-align: center;

    button {
      width: 300px;
      padding: 10px 0;
      color: #ffffff;
      border: none;
      font-size: 16px;
      background: linear-gradient(-45deg, #FF5252, #FF8C3C);
      box-shadow: 0px 4px 10px 0px rgba(255, 92, 78, 0.52);
      border-radius: 29px;
    }

  }

  .dialog {
    position: fixed;
    left: 0;
    top: 0;
    background: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    padding-top: 10vh;
    z-index: 999;

    .dialog_body {
      width: 80vw;
      margin: 0 auto;
      background-color: #ffffff;
      box-shadow: 0px 7px 43px 0px rgba(30, 30, 30, 0.05);
      border-radius: 3px;
      max-height: 500px;
      overflow: auto;

      .list_item {
        box-sizing: border-box;
        padding: 5px;
        border-bottom: solid 1px #EEEEEE;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .image {
          height: 32px;
          width: 67px;
        }

        .text {
          color: #686868;
          font-size: 10px;
          margin-left: 10px;
          text-align: left;
          flex: 1;
        }

        .active {
          padding: 0 5px;

          img {
            height: 10px;
            width: 13px;
          }
        }
      }
    }
  }
}
</style>
